package com.by.goods.service.impl;

import com.by.api.dto.CarDto;
import com.by.common.core.utils.Assert;
import com.by.feign.dto.SkuDto;
import com.by.goods.mapper.SkuMapper;
import com.by.goods.mapper.SpuMapper;
import com.by.goods.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SpuMapper spuMapper;

    @Override
    public SkuDto querySku(int id) {
        return skuMapper.querySku(id);
    }

    @Override
    public int lockSkuStock(List<CarDto> carDtoList) {
        //遍历更新库存
        if(null!=carDtoList&&carDtoList.size()>0){
            carDtoList.forEach(el->{
                //锁定sku的库存
                int tmp=skuMapper.lockStock(el);
                Assert.isZero(tmp,"sku库存锁定失败:"+el.toString());
                //锁定spu的库存
                tmp=spuMapper.lockSpuStock(el);
                Assert.isZero(tmp,"spu库存锁定失败:"+el.toString());
            });
        }
        return 1;
    }
}
